
//
//  BMUsefulPickerView.m
//  bigmama
//
//  Created by huyuchen on 2017/8/14.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMUsefulPickerView.h"

void ifDebug(dispatch_block_t blcok) {
#if DEBUG
    if (blcok) {
        blcok();
    }
#endif
}

@interface BMUsefulPickerView ()

@property (strong, nonatomic) UIView *pickerView;

@end

static const CGFloat kPickerViewHeight = 260.0f;

@implementation BMUsefulPickerView


+ (BMUsefulPickerView *)showSingleColPickerWithToolBarText:(NSString *)toolBarText data:(NSArray<NSString *> *)data defaultIndex:(NSInteger)defaultIndex toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)CancelBlock clickBlock:(SingleClickBlock)clickBlock{
    
    BMUsefulPickerView *usefulPickerView = [BMUsefulPickerView new];
    __weak BMUsefulPickerView *weakUsefulPickerView = usefulPickerView;
    
    BMSinglePickerView *single = [[BMSinglePickerView alloc] initWithToolBarText:toolBarText defaultIndex:defaultIndex data:data toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSString *selectedValue) {
        
    } cancelBlock:^{
        if (CancelBlock) {
            CancelBlock();
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    } doneBlock:^(NSInteger selectedIndex, NSString *selectedValue) {
        if (clickBlock) {
            clickBlock(selectedIndex,selectedValue);
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    }];
    
    usefulPickerView.pickerView = single;
    
    [usefulPickerView showPickerView];
    
    return usefulPickerView;
}


+ (BMUsefulPickerView *)showMultipleColPickerWithToolBarText:(NSString *)toolBarText defaultIndexs: (NSArray *)defaultIndexs data:(NSArray<NSArray<NSString *> *> *)data toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)CancelBlock clickBlock:(MultipleClickBlock)clickBlock{
    
    BMUsefulPickerView *usefulPickerView = [BMUsefulPickerView new];
    __weak BMUsefulPickerView *weakUsefulPickerView = usefulPickerView;
    
    BMMultiplePickerView *multiple = [[BMMultiplePickerView alloc] initWithToolBarText:toolBarText defaultIndexs:defaultIndexs data:data toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSArray *selectedValues) {
        
    } cancelBlock:^{
        if (CancelBlock) {
            CancelBlock();
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    } doneBlock:^(NSArray *selectedIndexs, NSArray *selectedValues) {
        if (clickBlock) {
            clickBlock(selectedIndexs,selectedValues);
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    }];
    
    usefulPickerView.pickerView = multiple;
    [usefulPickerView showPickerView];
    
    return usefulPickerView;
}


+ (BMUsefulPickerView *)showMultipleAssociatedColPickerWithToolBarText:(NSString *)toolBarText withDefaultValues: (NSArray *)defaultValues data:(NSArray *)data toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)CancelBlock clickBlock:(MultipleAssoCiatedClickBlock)clickBlock{
    
    BMUsefulPickerView *usefulPickerView = [BMUsefulPickerView new];
    __weak BMUsefulPickerView *weakUsefulPickerView = usefulPickerView;
    
    BMMultipleAssociatedPickerView *assoMultiple = [[BMMultipleAssociatedPickerView alloc] initWithToolBarText:toolBarText defaultValues:defaultValues data:data toolBarStyle:toolBarStyle valueDidChangedHandler:^(NSArray *selectedValues) {
        
    } cancelBlock:^{
        if (CancelBlock) {
            CancelBlock();
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    } doneBlock:^(NSArray *selectedValues) {
        if (clickBlock) {
            clickBlock(selectedValues);
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    }];
    
    usefulPickerView.pickerView = assoMultiple;
    [usefulPickerView showPickerView];
    
    return usefulPickerView;
}


+ (BMUsefulPickerView *)showDatePickerWithToolBarText:(NSString *)toolBarText withStyle:(BMDatePickerViewStyle *)style toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)CancelBlock clickBlock: (DateClickBlock)clickBlock valueChangeBlock:(DateDidChangedBlock)changeBlock{
    
    BMUsefulPickerView *usefulPickerView = [BMUsefulPickerView new];
    __weak BMUsefulPickerView *weakUsefulPickerView = usefulPickerView;
    
    BMNewDatePickerView *pickerView = [[BMNewDatePickerView alloc] initWithToolBarText:toolBarText pickerViewStyle:style toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSDate *selectedDate) {
        if (changeBlock) {
            changeBlock(selectedDate);
        }
    } cancelBlock:^{
        if (CancelBlock) {
            CancelBlock();
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    } doneBlock:^(NSDate *selectedDate) {
        if (clickBlock) {
            clickBlock(selectedDate);
        }
        __strong BMUsefulPickerView *strongUsefulPickerView = weakUsefulPickerView;
        if (strongUsefulPickerView) {
            // 移除
            [strongUsefulPickerView hidePickerView];
        }
    }];
    
    usefulPickerView.pickerView = pickerView;
    [usefulPickerView showPickerView];
    
    return usefulPickerView;
}

#pragma mark - life cycle
- (instancetype)init {
    if (self = [super init]) {
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapedSelf:)];
        tapGes.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tapGes];
        self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"BMUsefulPickerView dealloc");
}

- (void)tapedSelf:(UITapGestureRecognizer *)tap {
    CGPoint location = [tap locationInView:self];
    
    // 点击空白背景移除self
    if (location.y <= [UIScreen mainScreen].bounds.size.height - kPickerViewHeight) {
        [self hidePickerView];
    }
}

#pragma mark - helper
- (void)showPickerView {
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (window && !self.superview) {
        // 移除键盘第一响应者, 否则键盘始终会在界面的最上层
        [window endEditing:YES];
        /// 添加view
        [window addSubview:self];
        /// 设置pickerView的frame为屏幕底部外面 --- 动画初始位置
        self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, kPickerViewHeight);
        
        [UIView animateWithDuration:0.25f animations:^{
            self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
            /// 设置pickerView的动画结束位置, 显示在屏幕底部
            self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - kPickerViewHeight, [UIScreen mainScreen].bounds.size.width, kPickerViewHeight);
        } completion:^(BOOL finished) {
            [window bringSubviewToFront:self];
        }];
    }
}


- (void)hidePickerView {

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0 && [[UIDevice currentDevice].systemVersion floatValue] < 8.4){
        //适配8.1系统动画错位
        [UIView animateWithDuration:0.25f animations:^{
            self.pickerView.transform = CGAffineTransformMakeTranslation(0, kPickerViewHeight);
            self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1];
        } completion:^(BOOL finished) {
            self.pickerView.transform = CGAffineTransformIdentity;
            [self removeFromSuperview];
        }];
        
    }else{
        [UIView animateWithDuration:0.25f animations:^{
            self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1];
            self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, kPickerViewHeight);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            
        }];
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.superview) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, APP_HEIGHT);
        self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - kPickerViewHeight, [UIScreen mainScreen].bounds.size.width, kPickerViewHeight);
    }
}

#pragma mark - setter getter
- (void)setPickerView:(UIView *)pickerView {
    _pickerView = pickerView;
    [self addSubview:_pickerView];
}

- (BMPickerViewToolBar *)toolBar {
    if ([self.pickerView isKindOfClass:[BMSinglePickerView class]]) {
        return ((BMSinglePickerView *)self.pickerView).toolBar;
    }
    else if ([self.pickerView isKindOfClass:[BMMultiplePickerView class]]) {
        return ((BMMultiplePickerView *)self.pickerView).toolBar;
    }
    else if ([self.pickerView isKindOfClass:[BMMultipleAssociatedPickerView class]]) {
        return ((BMMultipleAssociatedPickerView *)self.pickerView).toolBar;
    }
    else if ([self.pickerView isKindOfClass:[BMNewDatePickerView class]]) {
        return ((BMNewDatePickerView *)self.pickerView).toolBar;
    }
    else {
        return [BMPickerViewToolBar new];
    }
}
@end
